<template>
  <el-container>
    <el-main>
      <div style="width: 100%;display: flex;align-items: flex-end;border-bottom: 1px #eee solid;margin-bottom: 20px">
        <span style="font-size: 18px;color:#409EFF;margin: 10px 0;">基本信息</span>
      </div>
      <el-form ref="sysPostForm" :model="sysPostForm" :rules="rules" label-width="80px">
        <el-col :span="14">
            <el-form-item label="岗位名称" prop="name">
              <el-input v-model="sysPostForm.name" v-focus></el-input>
            </el-form-item>
          <el-form-item label="备注">
            <el-input type="textarea" v-model="sysPostForm.remark"></el-input>
          </el-form-item>
        <el-form-item size="large" class="addBotton">
          <el-button type="primary" :loading="loading" @click="saveSysPost('sysPostForm')">立即创建</el-button>
        </el-form-item>
        </el-col>
      </el-form>
    </el-main>
  </el-container>
</template>

<script>
  import {save} from '@/api/sysPost'
  import utils from '../../utils/utils.js'
  import { EventBus } from '@/api/eventBus'
  export default {
    directives: {
      focus: {
        inserted: function (el) {
          el.querySelector('input').focus()
        }
      }
    },
    name:'sysPost-add',
    data() {
      return {
        sysPostForm: {
          name: '',
          status: 0,
          remark:'',
        },
        loading: false,
        rules: {
          name: [
            { required: true, message: '请输入组织名称', trigger: 'blur' },
            { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
          ],
        },
        inputType:false
      };
    },
    methods: {
      saveSysPost(sysPostForm) {
        var _this = this
        this.$refs[sysPostForm].validate((valid) => {
          if (valid) { // 校验表单
            _this.loading = true
            save(_this.sysPostForm).then(response => {
              console.log(response);
              _this.loading = false;
              _this.$message({
                message: response.content,
                offset: 120,
                center: true
              });
              EventBus.$emit('sysPost-add', response.data)
              _this.$store.dispatch('tagsView/delView', _this.$route).then(({ visitedViews }) => {
                _this.$router.push({
                  path: '/sysPost/list'
                })
              })// 关闭当前tab回到列表
            }).catch(error => {
              _this.loading = false;
              console.log('error!!')
            });
          } else {
            console.log('error submit!!')
            return false
          }
        })
      }
    },
  }
</script>

<style scoped>
  .el-headerBox {
    height: 50px !important;
    border-bottom: 1px solid #eee !important;
    line-height: 50px !important;
  }
  .addBotton{

  }
  .el-headerTitle{
    font-size: 20px;
    color: #409EFF;
  }
</style>
